package wrench.elevator.install.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.install.constant.Dict;
import com.wrench.elevator.install.constant.Result;


public abstract class WrenchElevatorInstallUtil
{
    private static final Logger logger = LoggerFactory.getLogger(WrenchElevatorInstallUtil.class);
    
    
    
    /**
     * 取参数
     */
    public static String getPara(HttpServletRequest request, String paraName)
    {
        try
        {
            request.setCharacterEncoding("utf-8");
        }
        catch (UnsupportedEncodingException e)
        {
            // TODO Auto-generated catch block
            logger.error("==> PbbUtil.getPara() -- request.setCharacterEncoding Error , error =>{}", e);
        }
        String paraValue = null;
        try
        {
            paraValue = request.getParameter(paraName);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            logger.error("==> PbbUtil.getPara() -- request.getParameter Error , error =>{}", e);
        }
        return paraValue;
    }
    
    
    /**
     * 判断手机格式是否正确 正确 true
     */
    public static boolean isVaildPhone(String phone)
    {
        return phone.matches(Dict.PHONE_REGEX);
    }

    /**
     * 是否有null对象 有 true
     */
    public static boolean hasNull(Object... objects)
    {
        for (Object object : objects)
        {

            if (object == null)
                return true;

        }
        return false;
    }

    /**
     * 判断对象是否相等 a传递字面量
     * */
    public static boolean objectEquals(Object a, Object b)
    {
        return a.equals(b);
    }
    
    /**
     * 判断两个字符串相等 相等 true
     * */
    public static boolean stringEquals(String a, String b)
    {
        if (hasNull(a, b))
            return false;
        return a.equals(b);
    }
    
    /**
     * 判断是否是md5加密后的密码
     * */
    public static boolean isEncode(String pwd)
    {
        return pwd.length() == 32;
    }

    /**
     * 判断字符串是否为 “” 是true
     * */
    public static boolean stringIsBlank(String... strings)
    {
        for (String string : strings)
        {
            if ("".equals(string.trim()))
                return true;
        }
        return false;
    }

    /**
     * 响应字符串
     */
    public static void responseStr(HttpServletResponse response, String str)
    {
        PrintWriter out = getWriter(response);
        if (!hasNull(out))
        {
            out.print(str);
        }
    }
    
    /**
     * 判断是否是一个整数
     */
    public static boolean isInt(String str)
    {
        return str.matches(Dict.INT_REGEX);
    }
    /**
     * 批量判断是否都是整数
     */
    public static boolean areInt(String... strs)
    {
        for (String str : strs)
        {
            if (!isInt(str))
                return false;
        }
        return true;
    }
    
    
    
    /**
     * getWriter()
     */
    private static PrintWriter getWriter(HttpServletResponse response)
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try
        {
            out = response.getWriter();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            logger.error("==> WrenchUtil.getWriter() -- response.getWriter() Error , error =>{}", e);
        }
        return out;
    }
    
    /**
     * 响应map
     */
    public static void responseMap(HttpServletResponse response, Map<String, Object> map)
    {
        String responseStr = null;
        try
        {
            responseStr = JSON.toJSONString(map);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            logger.error("==> WrenchUtil.responseMap() -- JSON.toJSONString Error , error =>{}", e);
        }
        if (!hasNull(responseStr))
            responseStr(response, responseStr);

    }

    /**
     * 调用存储过程需要的map k1 v1 k2 v2 k3 v3 ,,,
     *
     * @throws WrenchException
     * */
    public static ModelMap getParamMap(ModelMap map, Object... objects)
            throws WrenchElevatorInstallException
    {
        int len = objects.length;
        if (len % 2 != 0)
        {
            throw new WrenchElevatorInstallException(Result.WRONG_PARAM_NUM_CODE, Result.WRONG_PARAM_NUM_MESSAGE);
        }

        for (int i = 0; i < len; i += 2)
        {
            map.put(objects[i].toString(), objects[i + 1]);
        }
        System.out.println("map........................................" + map.get("face_count"));
        return map;
    }
}
